package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.pojo.ButtonPojo;
import com.xbongbong.paas.pojo.DetailTabPojo;
import com.xbongbong.paas.pojo.WorkFlowPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CommentUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleConfigDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleDetailDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleListDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleConfigVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleDetailHeaderVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleDetailVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleListDetailVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.DingBundleEntity;
import com.xbongbong.saas.enums.BusinessDetailTabEnum;
import com.xbongbong.saas.enums.ListTopButtonEnum;
import com.xbongbong.saas.enums.WorkFlowEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.mini.DingBundleTypeEnum;
import com.xbongbong.saas.model.DingBundleModel;
import com.xbongbong.saas.service.DingBundleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:13 下午
 */
@Service("dingBundleService")
public class DingBundleServiceImpl implements DingBundleService {
    @Resource
    private DingBundleModel dingBundleModel;
    
    private static final String templateid = "e2b2a4ce-6af7-4ca9-8244-d401766e1890";
    
    @Override
    public BundleListVO list(BundleListDTO bundleListDTO) throws XbbException {
        BundleListVO bundleListVO = new BundleListVO();
        List<BundleListDetailVO> list = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = bundleListDTO.getCorpid();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("page", bundleListDTO.getPage());
        param.put("status",1);
        param.put("type", DingBundleTypeEnum.SALES_GROUP.getCode());
        CommentUtil.addToMap(param, "ownerId", bundleListDTO.getOwnerId());
        CommentUtil.addToMap(param, "nameLike", bundleListDTO.getNameLike());
        param.put("del", 0);
        PageHelper pageHelper = PageHelperUtil.getPageHelper(param, dingBundleModel, bundleListDTO.getPageSize());
        List<DingBundleEntity> dingBundleEntityList = (List<DingBundleEntity>) PageHelperUtil.getEntityList(param, pageHelper, dingBundleModel);
        dingBundleEntityList.forEach(dingBundleEntity -> {
            Long id = dingBundleEntity.getId();
            BundleListDetailVO bundleListDetailVO = new BundleListDetailVO();
            BeanUtil.copyProperties(dingBundleEntity, bundleListDetailVO);
            bundleListDetailVO.setDataId(id);
            list.add(bundleListDetailVO);
        });
        List<ButtonPojo> topButtonList = new ArrayList<>();
        bundleListVO.setTopPermissions(getTopPermission(bundleListDTO,topButtonList));
        bundleListVO.setList(list);
        bundleListVO.setPageHelper(pageHelper);
        return bundleListVO;
    }
    
    private List<ButtonPojo> getTopPermission(BundleListDTO bundleListDTO,List<ButtonPojo> topButtonList){
        List<ListTopButtonEnum> topButtonEnumList = ListTopButtonEnum.getByBusinessType(XbbRefTypeEnum.SALE_BUNDLE.getCode());
        Set<String> permSet = bundleListDTO.getLoginUser().getPermSet();
        for (ListTopButtonEnum topButtonEnum : topButtonEnumList) {
            String permission = topButtonEnum.getProPermissionAlias();
            // permission为1的按钮一直存在的或还没加权限alias
            if (!permSet.contains(permission) && !Objects.equals(permission, StringConstant.POSITIVE_NUMBER)) {
                continue;
            }
            ButtonPojo buttonPojo = new ButtonPojo();
            BeanUtil.copyProperties(topButtonEnum.getAnEnum(), buttonPojo);
            topButtonList.add(buttonPojo);
        }
        return topButtonList;
    }
    
    @Override
    public BundleDetailHeaderVO getHeader(BundleDetailDTO bundleDetailDTO) throws XbbException {
        BundleDetailHeaderVO bundleDetailHeaderVO = new BundleDetailHeaderVO();
        DingBundleEntity dingBundleEntity = dingBundleModel.getEntity(bundleDetailDTO.getDataId(), bundleDetailDTO.getCorpid());
        if ( Objects.isNull(dingBundleEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018.getCode(),"数据不存在");
        }
        List<DetailTabPojo> tabPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<WorkFlowPojo> workFlowPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        handelDetailTab(bundleDetailDTO.getLoginUser(),tabPojoList,PlatFormEnum.WEB);
        handelDetailWorkFlow(bundleDetailDTO.getLoginUser(),workFlowPojoList);
        JSONObject header = new JSONObject();
        header.put("title",dingBundleEntity.getTitle());
        header.put("workFlow",workFlowPojoList);
        bundleDetailHeaderVO.setHeader(header);
        bundleDetailHeaderVO.setTab(tabPojoList);
        bundleDetailHeaderVO.setEntity(dingBundleEntity);
        return bundleDetailHeaderVO;
    }
    
    public void handelDetailTab(UserVO userVO,List<DetailTabPojo> tabPojoList, PlatFormEnum platFormEnum) throws XbbException {
        List<BusinessDetailTabEnum> tabEnumList;
        if (Objects.equals(platFormEnum, PlatFormEnum.WEB)) {
            tabEnumList = BusinessDetailTabEnum.getByBusinessType(XbbRefTypeEnum.SALE_BUNDLE.getCode());
        } else {
            tabEnumList = BusinessDetailTabEnum.getByBusinessTypeAndPermissionForDingtalk(XbbRefTypeEnum.SALE_BUNDLE.getCode(), userVO.getPermSet());
        }
        for (BusinessDetailTabEnum tabEnum : tabEnumList) {
            DetailTabPojo detailTabPojo = new DetailTabPojo();
            BeanUtil.copyProperties(tabEnum.getAnEnum(), detailTabPojo);
            detailTabPojo.setAttr(detailTabPojo.getKey());
            tabPojoList.add(detailTabPojo);
        }
    }
    
    public void handelDetailWorkFlow(UserVO userVO,List<WorkFlowPojo> workFlowPojoList) throws XbbException {
        Set<String> permSet = userVO.getPermSet();
        List<WorkFlowEnum> workFlowEnumList = WorkFlowEnum.getByBusinessTypeAndPermission(XbbRefTypeEnum.SALE_BUNDLE.getCode(), permSet);
        for (WorkFlowEnum workFlowEnum : workFlowEnumList) {
            WorkFlowPojo workFlowPojo = new WorkFlowPojo();
            BeanUtil.copyProperties(workFlowEnum, workFlowPojo);
            workFlowPojoList.add(workFlowPojo);
        }
    }
    
    @Override
    public BundleDetailVO get(BundleDetailDTO bundleDetailDTO) throws XbbException {
        BundleDetailVO bundleDetailVO = new BundleDetailVO();
        DingBundleEntity dingBundleEntity = dingBundleModel.getEntity(bundleDetailDTO.getDataId(), bundleDetailDTO.getCorpid());
        if ( Objects.isNull(dingBundleEntity) ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018.getCode(),"数据不存在");
        }
        BeanUtil.copyProperties(dingBundleEntity, bundleDetailVO);
        bundleDetailVO.setDataId(dingBundleEntity.getId());
        return bundleDetailVO;
    }
    
    @Override
    public BundleConfigVO getConfig(BundleConfigDTO bundleConfigDTO) throws XbbException {
        return null;
    }
}
